<template>
  <div>
    <el-form label-width="130px" style="width:500px" ref="apkDetailInfo" :rules="rules" :model="apkDetailInfo">
      <el-form-item label="Apk分类" prop="apkKey">
        <el-select v-model="apkDetailInfo.apkKey" placeholder="选择分类" @change="getNotForceList">
          <el-option label="Android" value="anpxdAndroidErpApp"></el-option>
          <el-option label="IOS" value="anpxdIOSErpApp"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="Apk版本号:" prop="apkVersion">
        <el-input clearable placeholder="请输入Apk版本号" v-model="apkDetailInfo.apkVersion" @blur="validateVersion"></el-input>
      </el-form-item>
      <el-form-item label="Apk下载地址:" prop="apkUrl">
        <el-input clearable placeholder="请输入Apk下载地址" v-model="apkDetailInfo.apkUrl"></el-input>
      </el-form-item>
      <el-form-item label="Apk名称:" prop="apkName">
        <el-input clearable placeholder="请输入Apk名称" v-model="apkDetailInfo.apkName"></el-input>
      </el-form-item>
      <el-form-item label="Apk热更新版本号:">
        <el-input clearable placeholder="请输入Apk热更新版本号" v-model="apkDetailInfo.apkHotVersion"></el-input>
      </el-form-item>
      <el-form-item label="Apk备注:">
        <el-input clearable placeholder="请输入Apk备注" v-model="apkDetailInfo.apkRemark"></el-input>
      </el-form-item>
      <el-form-item label="选择强制更新版本:">
        <el-checkbox v-for="(item,index) in versionList" :key="index" :label="item.apkId" v-model="forceList">{{item.apkVersion}}</el-checkbox>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" v-loading="loading" @click="submitForm('apkDetailInfo')">{{buttonName}}</el-button>
        <el-button v-show="true" @click="cancelEdit()">返回列表</el-button>
        <el-button @click="resetForm()" v-if="!apkParam.apkKey">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    name: "versionForm",
    data(){
      return {
        apkDetailInfo:{},
        rules: {
          apkKey: [
            { required: true, message: '请选择Apk分类', trigger: 'blur' },
          ],
          apkVersion: [
            { required: true, message: '版本号只支持数字和字符.', trigger: 'blur'},
          ],
          apkUrl: [
            { required: true, message: '请输入Apk下载地址', trigger: 'blur' },
          ],
          apkName: [
            { required: true, message: '请输入Apk名称', trigger: 'blur' },
          ]
        },
        versionList:[],
        forceList:[],
        loading:false,
        buttonName:'立即创建',
      }
    },
    computed:{
      apkParam(){
        return this.$route.query;
      }
    },
    watch:{
      'apkParam'(){
        this.forceList = [];
        if(this.apkParam.apkKey){
          this.getVersionList();
          this.buttonName = '立即更新';
        }else{
          this.apkDetailInfo = {};
          this.versionList = [];
          this.buttonName = '立即创建';
        }
      }
    },
    created(){
      if(this.apkParam.apkKey){
        this.buttonName = '立即更新';
        this.getVersionList();
      }else{
        this.apkDetailInfo = {};
        this.versionList = [];
        this.buttonName = '立即创建';
      }
    },
    methods:{
      validateVersion(){
        var reg = /[^\d,\.]+/;
        if(reg.exec(this.apkDetailInfo.apkVersion)){
          this.$message({
            type:'warning',
            message:'版本号格式输入不正确'
          });
          return false
        }else{
          return true
        }
      },
      getVersionList(){
        axios.get(this.$api.getApkList,{params:this.apkParam})
          .then(res=>{
            this.apkDetailInfo = res.data.result[0];
            this.getNotForceList();
          })
      },
      getNotForceList(){
        var forceParam = {apkForce:0,apkKey:this.apkDetailInfo.apkKey};
        if(this.apkDetailInfo.apkKey==='anpxdAndroidErpApp'){
          this.apkDetailInfo.apkUrl = 'https://www.anpxd.com/apk/erp/qiyuan.apk'
        }else if(this.apkDetailInfo.apkKey==='anpxdIOSErpApp'){
          this.apkDetailInfo.apkUrl = 'https://itunes.apple.com/us/app/%E5%90%AF%E8%BE%95%E6%B1%BD%E8%BD%A6/id1352712474?mt=8&uo=4'
        }
        axios.get(this.$api.getApkList,{params:forceParam})
          .then(res=>{
            this.versionList = res.data.result;
          })
      },
      submitForm(formName){
        this.loading = true;
        delete this.apkDetailInfo.apkForce;
        this.apkDetailInfo.versionList = this.forceList.join(',');
        this.$refs[formName].validate((valid) => {
          if (valid&&this.validateVersion()) {
            if(!this.apkParam.apkKey){
              this.insertApp();
            }else{
              this.updateApp();
            }
          } else {
            this.$message({
              type:'warning',
              message:'提交失败'
            });
            this.loading = false;
            return false;
          }
        });
      },
      insertApp(){
        delete this.apkDetailInfo.apkId;
        axios.get(this.$api.insertApk,{params:this.apkDetailInfo})
          .then(res=>{
            this.loading = false;
            this.$message({
              type:res.data.code===1?'success':'warning',
              message:res.data.msg
            });
            if(res.data.code===1){
              this.$router.push('/version/versionList')
            }else{
              return false
            }
          });
      },
      updateApp(){
        axios.get(this.$api.updateApk,{params:this.apkDetailInfo})
          .then(res=>{
            this.loading = false;
            this.$message({
              type:res.data.code===1?'success':'warning',
              message:res.data.msg
            });
            if(res.data.code===1){
              this.$router.push('/version/versionList')
            }else{
              return false
            }
          });
      },
      cancelEdit(){
        this.$router.push('/version/versionList')
      },
      resetForm(){
        for(var i in this.apkDetailInfo){
          this.apkDetailInfo[i] = ''
        }
      }
    }
  }
</script>

<style scoped>

</style>
